package com.monter.carlo.poker.net.client.impl;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;

import com.monter.carlo.poker.domain.ServiceResult;
import com.monter.carlo.poker.domain.exception.TexasNetException;
import com.monter.carlo.poker.net.client.ITexasSocketClient;
import com.monter.carlo.poker.net.domain.TexasMessageVO;

/**
 * @author Seven
 * @since 2012-03-23
 */
public class DefaultTexasSocketClient implements ITexasSocketClient {

	private Socket socket;

	public DefaultTexasSocketClient() {
		init();
	}

	private void init() {
		try {
			socket = new Socket("127.0.0.1", 7777);
		} catch (Exception e) {
			throw new TexasNetException("InitSocketClientException", e);
		}
	}

	public void close() {
		try {
			socket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public ServiceResult sendMessage(TexasMessageVO message) {
		OutputStream outs;
		try {
			outs = socket.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outs);
			oos.writeObject(message);
			oos.flush();
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
			return new ServiceResult("TexasSendMsgNetError,msg="
					+ e.getMessage());
		}
		return ServiceResult.SUCCESS;
	}

}
